package com.copa.l283;

import java.util.Arrays;

/**
 * @author copa
 * @createDate 2022-06-02 16:07
 * @function 一次遍历法 针对Test1的moveZeroes2(int[] nums)进行优化
 */
public class Test2 {

    public void moveZeroes(int[] nums) {
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                // 不等于0的放左边，等于0的放右边
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j++] = tmp;
            }
        }
    }


    public static void main(String[] args) {
        int[] nums = {0,1,0,3,12};
        //int[] nums = {0,0,1};
        new Test2().moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }
}
